import UIKit

class GameViewController: UIViewController {
    var cellRowCount = 4
    var score = 0 {
        didSet{
            scoreView?.text = "SCORE:\(score)"
        }
    }
    

func saveHighScore(newScore: Int) {
    let defaults = UserDefaults.standard
    let highScore = defaults.integer(forKey: "highScore")
    if newScore > highScore {
        defaults.set(newScore, forKey: "highScore")
    }
}

    
    var scoreView : UILabel?

    override func viewDidLoad() {

        super.viewDidLoad()

        view.backgroundColor = UIColor.white
        setupUI()
        // 创建背景图片视图并设置图片
           let backgroundImageView = UIImageView(frame: view.bounds)
           backgroundImageView.image = UIImage(named: "2")
           backgroundImageView.contentMode = .scaleToFill // 或者选择其他适合的模式
        
        // 将背景图片视图插入到视图层次的底部，确保它不会盖其他视图，如按钮等
           view.insertSubview(backgroundImageView, at: 0)
            }
    
    private func setupUI() {
        
        
        let boardWH = SCREEN_WIDTH - boardLeftMargin * 2
        let gameBoard = GameBoard(frame: CGRect(x: 0, y: 0, width: boardWH, height: boardWH),cellRowCount: cellRowCount)
            gameBoard.center = view.center
        var cellWH = (gameBoard.frame.width - margin * CGFloat(cellRowCount + 1)) / CGFloat(cellRowCount)
        var scoreW = cellWH * 3
        var scoreH = cellWH * 0.9
       

        

gameBoard.gameOver = {
    self.saveHighScore(newScore: self.score)
    let highScore = UserDefaults.standard.integer(forKey: "highScore")
    let alertContr = UIAlertController(title: "Game Over", message:"Your score: \(self.score). Highest score: \(highScore)", preferredStyle: .alert)
    let confirmAction = UIAlertAction(title: "OK", style: .default, handler: { (alertAction) in
        gameBoard.resetGame()
        self.score = 0
    })
    alertContr.addAction(confirmAction)
    self.present(alertContr, animated: true, completion: nil)
}
        

        gameBoard.scoreChanged = { score in
            self.score = score
        }
        view.addSubview(gameBoard)
        
        cellWH = (gameBoard.frame.width - margin * CGFloat(5)) / CGFloat(4)
        scoreW = cellWH * 3
        scoreH = cellWH * 0.9
        scoreView = UILabel(frame: CGRect(x: 0, y: 0, width: scoreW, height: scoreH))
        scoreView?.center = CGPoint(x: SCREEN_WIDTH / 2, y: gameBoard.frame.minY - margin * 2 - scoreH / 2)
        scoreView?.font = UIFont.boldSystemFont(ofSize: 20)
        scoreView?.textAlignment = .center
        scoreView?.textColor = UIColor.white
        scoreView?.backgroundColor = UIColor.gray
        scoreView?.text = "SCORE:0"
        scoreView?.layer.cornerRadius = 8
        scoreView?.clipsToBounds = true
        view.addSubview(scoreView!)
        let btn = UIButton(frame: CGRect(x: 0, y: 0, width: 100, height: 70))
            btn.setTitle("Undo", for: .normal)
        btn.setTitleColor(UIColor.black, for: .normal)
            btn.titleLabel?.font = UIFont(name: "Marker Felt", size: 36)
            btn.center = CGPoint(x: view.center.x-85, y: view.center.y + 180) // Adjust this to place it correctly
            btn.addTarget(gameBoard, action: #selector(GameBoard.undoLastMove), for: .touchUpInside)
            view.addSubview(btn)
        let btn1 = UIButton(frame: CGRect(x: 0, y: 0, width: 130, height: 70))
            btn1.setTitle("Restart", for: .normal)
        btn1.setTitleColor(UIColor.black, for: .normal)
            btn1.titleLabel?.font = UIFont(name: "Marker Felt", size: 36)
            btn1.center = CGPoint(x: view.center.x+85, y: view.center.y + 180) // Adjust this to place it correctly
            btn1.addTarget(gameBoard, action: #selector(GameBoard.resetGame), for: .touchUpInside)
            view.addSubview(btn1)
    
}}
